# -*- coding: utf-8 -*-
from config import DEFAULT_USER_PORTRAIT_PATH
from database import Base, db_session
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean
from sqlalchemy.orm import relationship, backref
from datetime import datetime

class BaseModel():
    @classmethod
    def get(cls, id):
        return cls.query.get(id)
    def save(self):
        if self.id is None:
            db_session.add(self)
        db_session.commit()

class User(Base, BaseModel):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique=True)
    portrait = Column(String(100), default=DEFAULT_USER_PORTRAIT_PATH)
    discriminator = Column('type', String(50))
    __mapper_args__ = {'polymorphic_on': discriminator}
    def __init__(self, name, portrait):
        self.name = name
        self.portrait = portrait
    def __repr__(self):
        return self.name
    
class RenrenUser(User):
    __tablename__ = 'renren_user'
    __mapper_args__ = {'polymorphic_identity': 'renren'}
    id = Column(Integer, ForeignKey('user.id'), primary_key=True)
    renren_id = Column(Integer)
    def __init__(self, name, portrait, id):
        self.renren_id = id
        super(RenrenUser, self ).__init__(name, portrait)
        
class Survey(Base, BaseModel):
    __tablename__ = 'survey'
    id = Column(Integer, primary_key=True)
    title = Column(String(140))
    owner_id = Column(Integer, ForeignKey('user.id'))
    owner = relationship('User', backref='surveys_owned')
    questions = relationship('Question', backref='survey')
    question_count = Column(Integer)
    create_time = Column(DateTime, default=datetime.now)

class Question(Base):
    __tablename__ = 'question'
    id = Column(Integer, primary_key=True)
    survey_id = Column(Integer, ForeignKey('survey.id'))
    options = relationship('Option', backref='question')
    content = Column(String(140))
    
class Option(Base):
    __tablename__ = 'option'
    id = Column(Integer, primary_key=True)
    question_id = Column(Integer, ForeignKey('question.id'))
    content = Column(String(140))
    correctness = Column(Boolean, default=False)
    
class SurveyResult(Base, BaseModel):
    __tablename__ = 'survey_result'
    id = Column(Integer, primary_key=True)
    survey_id = Column(Integer, ForeignKey('survey.id'))
    survey =  relationship('Survey', backref='results')
    respondent_id = Column(Integer, ForeignKey('user.id'))
    respondent = relationship('User', backref='surveys_responded')
    score = Column(Integer)
    response_time = Column(DateTime, default=datetime.now)
    responses = relationship('Response', backref='survey_result')
    
class Response(Base):
    __tablename__ = 'response'
    survey_result_id = Column(Integer, ForeignKey('survey_result.id'), primary_key=True)
    question_id = Column(Integer, ForeignKey('question.id'), primary_key=True)
    question = relationship('Question', backref='responses')
    option_id = Column(Integer, ForeignKey('option.id'))
    option = relationship('Option', backref='responses')

    